﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using System.Collections.Generic;
using KenLib.BusinessObject;
using KenLib.BusinessManager;

public partial class JOINT_frmJ_TraineeWorkedCompInput : System.Web.UI.Page
{
    応募会社履歴情報Manager _応募会社履歴情報Manager = new 応募会社履歴情報Manager();
    応募情報Manager _応募情報Manager = new 応募情報Manager();
    本社情報Manager _本社情報Manager = new 本社情報Manager();

    protected void Page_PreRender(object sender, EventArgs e)
    {
        ViewState["frmJ_TraineeWorkedCompInput_Refresh"] = Session["frmJ_TraineeWorkedCompInput_Refresh"];
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("来社後　職歴情報登録");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeWorkedCompInput");
        if (Session["ID_frmJ_TraineeResumeAInput"] == null)
        {
            Response.Redirect("frmJ_TraineeAcademicInput.aspx");
        }
        RFV_会社名.ErrorMessage = GetError("ER00007");
        RFV_仕事内容.ErrorMessage = GetError("ER00001");
        RFV_入社年月日月.ErrorMessage = GetError("ER00007");
        RFV_入社年月日年.ErrorMessage = GetError("ER00007");
        RFV_退社年月日月.ErrorMessage = GetError("ER00007");
        RFV_退社年月日年.ErrorMessage = GetError("ER00007");
        CV_退社年月日年.ErrorMessage = GetError("ER00005");
        CV_退社年月日月.ErrorMessage = GetError("ER00005");
        if (!IsPostBack)
        {
            Session["frmJ_TraineeWorkedCompInput_Refresh"] = Server.UrlEncode(System.DateTime.Now.ToString());
            ddl_会社名.Focus();
            ViewState["pageindex"] = 0;
            DataTable dt = frmJ_TraineeWorkedCompInput_Select生年月日_応募情報(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
            Load_ddl_入社年月日月();            
            Load_ddl_入社年月日年(Convert.ToDateTime(dt.Rows[0]["生年月日"]).Year);
            Load_ddl_退社年月日月();
            Load_ddl_退社年月日年(Convert.ToDateTime(dt.Rows[0]["生年月日"]).Year);
            Load会社名();
            ShowGrv(Convert.ToInt32( Session["ID_frmJ_TraineeResumeAInput"]));
        }
        
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Go to page frmJ_TraineeInterviewList.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnキャンセル_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_TraineeInterviewList.aspx");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Envet: Go to page frmJ_TraineeResumeTranslate.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn翻訳_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_TraineeResumeTranslate.aspx");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_入社年月日月
    /// </summary>
    public void Load_ddl_入社年月日月()
    {
        List<string> list = new List<string>();
        for (int i = 1; i < 13; i++)
        {
            list.Add(i.ToString());
        }
        ddl_入社年月日月.DataSource = list;
        ddl_入社年月日月.DataBind();
        ListItem item = new ListItem("[月]", "-1");
        item.Selected = true;
        ddl_入社年月日月.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_入社年月日年
    /// </summary>
    public void Load_ddl_入社年月日年(int int_YearInput)
    {
        List<string> list = new List<string>();
        for (int i = int_YearInput; i <= DateTime.Now.Year; i++)
        {
            list.Add(i.ToString());
        }
        ddl_入社年月日年.DataSource = list;
        ddl_入社年月日年.DataBind();
        ListItem item = new ListItem("[年]", "-1");
        item.Selected = true;
        ddl_入社年月日年.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_退社年月日月
    /// </summary>
    public void Load_ddl_退社年月日月()
    {
        List<string> list = new List<string>();
        for (int i = 1; i < 13; i++)
        {
            list.Add(i.ToString());
        }
        ddl_退社年月日月.DataSource = list;
        ddl_退社年月日月.DataBind();
        ListItem item = new ListItem("[月]", "-1");
        item.Selected = true;
        ddl_退社年月日月.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_退社年月日年
    /// </summary>
    public void Load_ddl_退社年月日年(int int_YearInput)
    {
        
        List<string> list = new List<string>();
        for (int i = int_YearInput; i <= DateTime.Now.Year; i++)
        {
            list.Add(i.ToString());
        }
        ddl_退社年月日年.DataSource = list;
        ddl_退社年月日年.DataBind();
        ListItem item = new ListItem("[年]", "-1");
        item.Selected = true;
        ddl_退社年月日年.Items.Insert(0, item);
    }

    

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check insert datatable in table 応募会社履歴情報
    /// </summary>
    /// <param name="_応募会社履歴情報"></param>
    /// <returns></returns>
    public int frmJ_TraineeWorkedCompInput_Insert(応募会社履歴情報 _応募会社履歴情報)
    {
        if (_応募会社履歴情報Manager.frmJ_TraineeWorkedCompInput_Insert(_応募会社履歴情報) == 1)
        {
            return 1;
        }
        return 0;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check update datatable in table 応募会社履歴情報
    /// </summary>
    /// <param name="_応募会社履歴情報"></param>
    /// <returns></returns>
    public int frmJ_TraineeWorkedCompInput_Update(応募会社履歴情報 _応募会社履歴情報)
    {
        if (_応募会社履歴情報Manager.frmJ_TraineeWorkedCompInput_Update(_応募会社履歴情報) == 1)
        {
            return 1;
        }
        return 0;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load empty gridview
    /// </summary>
    /// <param name="grv"></param>
    /// <param name="strTableHeader"></param>
    /// <param name="strEmpty"></param>
    public void ShowGridViewNotClassEmpty(GridView grv, string strTableHeader, string strEmpty)
    {
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        foreach (string str in arrColumnNames)
        {
            dt.Columns.Add(str);
        }
        grv.ShowFooter = false;
        DataRow r = dt.NewRow();
        dt.Rows.InsertAt(r, 0);
        grv.DataSource = dt;
        grv.DataBind();
        grv.Rows[0].Cells.Clear();
        TableCell cell = new TableCell();
        cell.ColumnSpan = grv.Columns.Count + 1;
        cell.Text = strEmpty;
        grv.Rows[0].Cells.Add(cell);

    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in gridview 
    /// </summary>
    /// <param name="id"></param>
    public void ShowGrv(int id)
    {
        DataTable dt = new DataTable();
        dt = _応募会社履歴情報Manager.frmJ_TraineeWorkedCompInput_SelectByID(id);
        if (dt.Rows.Count == 0)
        {
            string strHeader = "会社名,部門名,開始年月日,終了年月日";
            ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");
        }
        else
        {
            grv.DataSource = dt;
            grv.DataBind();
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Double in gridview with RowDataBound
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("ondblclick", "javascript:__doPostBack('ctl00$ContentPlaceHolder1$grv','DoubleClick$0" + e.Row.RowIndex + "')");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Double in gridview with RowCommand
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DoubleClick" && e.CommandArgument.ToString()!="0-1")
        {
            int numberpager = Convert.ToInt32(ViewState["pageindex"]);
            int intRows = 0;
            if (numberpager == 0)
            {
                intRows = Convert.ToInt32(e.CommandArgument);
            }
            else
            {
                intRows = Convert.ToInt32(e.CommandArgument) + (numberpager * 10);
            }

            DataTable dt = new DataTable();
            //Gan cung id =60, khi nao xong nho sua lai
            dt = _応募会社履歴情報Manager.frmJ_TraineeWorkedCompInput_SelectByID(Convert.ToInt32( Session["ID_frmJ_TraineeResumeAInput"]));
            if (dt.Rows.Count > 0)
            {
                //会社名
                if (dt.Rows[intRows]["会社名"].ToString() != "")
                {
                    string str_会社名 = dt.Rows[intRows]["会社名"].ToString();
                    ListItem item = ddl_会社名.Items.FindByText(str_会社名);
                    ddl_会社名.SelectedIndex = ddl_会社名.Items.IndexOf(item);
                }
                else
                {
                    ddl_会社名.SelectedValue = "-1";
                }
                //txt_会社名.Text = ConvertTexttoHTML(dt.Rows[intRows]["会社名"].ToString());

                //部門名			
                txt_部門名.Text = ConvertTexttoHTML(dt.Rows[intRows]["部門名"].ToString());

                //仕事内容
                txt_仕事内容.Text = ConvertTexttoHTML(dt.Rows[intRows]["仕事内容"].ToString());

                //主な実績
                txt_主な実績.Text = ConvertTexttoHTML(dt.Rows[intRows]["主な実績"].ToString());

                //入社年月日
                if (dt.Rows[intRows]["開始年月日"].ToString() != "")
                {
                    string str_開始年月日 = dt.Rows[intRows]["開始年月日"].ToString();
                    string str_開始年月日_year = str_開始年月日.Substring(0, 4);
                    string str_開始年月日_month = str_開始年月日.Substring(5, 2);
                    if (Convert.ToInt32(str_開始年月日_month) < 10)
                    {
                        str_開始年月日_month = str_開始年月日_month.Substring(1, 1);
                    }
                    ListItem item = ddl_入社年月日月.Items.FindByText(str_開始年月日_month);
                    ddl_入社年月日月.SelectedIndex = ddl_入社年月日月.Items.IndexOf(item);
                    ListItem item1 = ddl_入社年月日年.Items.FindByText(str_開始年月日_year);
                    ddl_入社年月日年.SelectedIndex = ddl_入社年月日年.Items.IndexOf(item1);
                }

                //退社年月日
                if (dt.Rows[intRows]["終了年月日"].ToString() != "")
                {
                    string str_終了年月日 = dt.Rows[intRows]["終了年月日"].ToString();
                    string str_終了年月日_year = str_終了年月日.Substring(0, 4);
                    string str_終了年月日_month = str_終了年月日.Substring(5, 2);
                    if (Convert.ToInt32(str_終了年月日_month) < 10)
                    {
                        str_終了年月日_month = str_終了年月日_month.Substring(1, 1);
                    }
                    ListItem item = ddl_退社年月日月.Items.FindByText(str_終了年月日_month);
                    ddl_退社年月日月.SelectedIndex = ddl_退社年月日月.Items.IndexOf(item);
                    ListItem item1 = ddl_退社年月日年.Items.FindByText(str_終了年月日_year);
                    ddl_退社年月日年.SelectedIndex = ddl_退社年月日年.Items.IndexOf(item1);
                }

                //Lay Id de update
                ViewState["DoubleClick_ID"] = dt.Rows[intRows]["ID"];

                //Xet truong hop add hoac update
                ViewState["Mode"] = "Update";
                btn_登録.Text = "更新";
            }
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: paging
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ViewState["pageindex"] = e.NewPageIndex;
        grv.PageIndex = e.NewPageIndex;
        //Gan cung ID khi nao xong thay doi lai gia tri
        ShowGrv(Convert.ToInt32( Session["ID_frmJ_TraineeResumeAInput"]));
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get object 応募会社履歴情報
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public 応募会社履歴情報 Get応募会社履歴情報(int temp)
    {
        応募会社履歴情報 _応募会社履歴情報 = new 応募会社履歴情報();

        //会社名
        _応募会社履歴情報.会社名 = ConvertHTMLtoText(ddl_会社名.SelectedItem.Text);

        //部門名
        _応募会社履歴情報.部門名 = ConvertHTMLtoText(txt_部門名.Text);

        //仕事内容
        _応募会社履歴情報.仕事内容 = ConvertHTMLtoText(txt_仕事内容.Text);

        //主な実績
        _応募会社履歴情報.主な実績 = ConvertHTMLtoText(txt_主な実績.Text);

        //開始年月日
        DateTime dt_開始年月日 = new DateTime(Convert.ToInt32(ddl_入社年月日年.SelectedValue), Convert.ToInt32(ddl_入社年月日月.SelectedValue), 1);
        _応募会社履歴情報.開始年月日 = dt_開始年月日;

        //終了年月日
        DateTime dt_終了年月日 = new DateTime(Convert.ToInt32(ddl_退社年月日年.SelectedValue), Convert.ToInt32(ddl_退社年月日月.SelectedValue), 1);
        _応募会社履歴情報.終了年月日 = dt_終了年月日;

        //応募情報ID Nho sua lai so 60 khi lam xong
        _応募会社履歴情報.応募情報ID = Convert.ToInt32( Session["ID_frmJ_TraineeResumeAInput"]);

        //廃止区分
        _応募会社履歴情報.廃止区分 = 0;

        //企業ID
        _応募会社履歴情報.企業ID = Convert.ToInt32(ddl_会社名.SelectedValue);
        //Option update
        if (temp == 1)
        {
            _応募会社履歴情報.ID = Convert.ToInt32(ViewState["DoubleClick_ID"]);
        }
        return _応募会社履歴情報;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character html to text
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertHTMLtoText(string temp)
    {
        temp = temp.Replace("<", "&lt;");
        temp = temp.Replace(">", "&gt;");
        temp = temp.Replace(Convert.ToString((char)13), "<br/>");
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Insert and Update data in table 応募会社履歴情報
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        if (Convert.ToString(ViewState["Mode"]).Equals(""))
        {
            if (Session["frmJ_TraineeWorkedCompInput_Refresh"].ToString() == ViewState["frmJ_TraineeWorkedCompInput_Refresh"].ToString())
            {
                応募会社履歴情報 _応募会社履歴情報 = Get応募会社履歴情報(0);
                if (frmJ_TraineeWorkedCompInput_Insert(_応募会社履歴情報) == 1)
                {
                    ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                    ClearText();
                    Session["frmJ_TraineeWorkedCompInput_Refresh"] = Server.UrlEncode(System.DateTime.Now.ToString());
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('データベースに接続ができません');</script>");
                }
            }
            else
            {
                ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                ClearText();
            }
           
        }
        else
        {
            //update
            応募会社履歴情報 _応募会社履歴情報 = Get応募会社履歴情報(1);
            if (frmJ_TraineeWorkedCompInput_Update(_応募会社履歴情報) == 1)
            {
                ClearText();
                ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                btn_登録.Text = "登録";
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('データベースに接続ができません');</script>");
            }
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Go to page frmJ_TraineeWorkedCompInput.aspx
    /// </summary>
    public void ClearText()
    {
        ViewState.Remove("DoubleClick_ID");
        ViewState.Remove("Mode");
        Response.Redirect("frmJ_TraineeWorkedCompInput.aspx");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Go to page frmJ_TraineeWorkedCompInput.aspx
    /// </summary>
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        ViewState.Remove("DoubleClick_ID");
        ViewState.Remove("Mode");
        Response.Redirect("frmJ_TraineeWorkedCompInput.aspx");
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character text to html
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertTexttoHTML(string temp)
    {
        temp = temp.Replace("&lt;", "<");
        temp = temp.Replace("&gt;", ">");
        return temp;
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get error message from id
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            error = dt.Rows[0][2].ToString();
        }
        return error;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Show length string when string too long
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ShowTextLength(string temp)
    {
        //tieng viet
        if (temp.Length > 28)
        {
            temp = temp.Substring(0, 28) + "...";
        }
        return temp;

    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get 1 record table 応募情報
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public DataTable frmJ_TraineeWorkedCompInput_Select生年月日_応募情報(int ID)
    {
        return _応募情報Manager.frmJ_TraineeWorkedCompInput_Select生年月日_応募情報(ID);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get data load dropdownlist 会社名
    /// </summary>
    /// <returns></returns>
    public DataTable frmJ_TraineeWorkedCompInput_Select会社名_本社情報()
    {
        return _本社情報Manager.frmJ_TraineeWorkedCompInput_Select会社名_本社情報();
    }

    
    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data for dropdownlist 会社名
    /// </summary>
    public void Load会社名() 
    {
        DataTable dt = frmJ_TraineeWorkedCompInput_Select会社名_本社情報();
        ddl_会社名.DataSource = dt;
        ddl_会社名.DataTextField = "企業名";
        ddl_会社名.DataValueField = "企業ID";
        ddl_会社名.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_会社名.Items.Insert(0, item);
    }
    
}
